<?php
global $prefix,$pdo,$dbname;

$config = require_once('./appct/Common/Conf/config.php');
$servername = $config['DB_HOST'].':'.$config['DB_PORT'];
$username = $config['DB_USER'];
$password = $config['DB_PWD'];
$dbname = $config['DB_NAME'];
$prefix = $config['DB_PREFIX']; 

try {
    $pdo = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $pdo->exec("SET character_set_connection=utf8, character_set_results=utf8, character_set_client=binary");
} catch(PDOException $e) {
    echo $sql . "<br>" . $e->getMessage();exit;
}

//判断表存在 
function pdo_tableexists($table){
	$table = $GLOBALS['prefix'].$table;	
	$result = $GLOBALS['pdo']->query("select count(*) as column_name from information_schema.columns where table_schema = '".$GLOBALS['dbname']."' and table_name = '".$table."'")->fetch();
	$column_name = $result['column_name'];
	if(empty($column_name)){
		return false;
	}else{
		return true;
	}	
}

//判断表字段
function pdo_fieldexists($table,$fieldname){
	$table = $GLOBALS['prefix'].$table;	
	$result = $GLOBALS['pdo']->query("select count(*) as column_name from information_schema.columns where table_schema = '".$GLOBALS['dbname']."' and table_name = '".$table."' and column_name = '".$fieldname."'")->fetch();;
	$column_name = $result['column_name'];
	if(empty($column_name)){
		return false;
	}else{
		return true;
	}
}

//获取表名 
function tablename($table){
	return $GLOBALS['prefix'].$table;
} 

//执行
function pdo_query($sql){
	$GLOBALS['pdo']->exec($sql);
} 

//执行
function pdo_run($sql){
	$GLOBALS['pdo']->exec($sql);
} 

if(!pdo_tableexists('sxkj_offline_admin')){
    $sql = "CREATE TABLE  ".tablename('sxkj_offline_admin')." (
		   	`id` int(11) NOT NULL AUTO_INCREMENT,
  			`username` varchar(255) DEFAULT '',
  			`password` varchar(255) DEFAULT '',
  			`add_time` int(11) DEFAULT '0',
		    `update_time` int(11) DEFAULT '0',
		    `last_time` int(11) DEFAULT '0',
		  PRIMARY KEY (`id`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
	pdo_query($sql);
	$sql = "INSERT INTO ".tablename('sxkj_offline_admin')." VALUES ('1', 'admin', 'e10adc3949ba59abbe56e057f20f883e', '1618747991', '1618747991','0');";
	pdo_run($sql);	
}

if(!pdo_tableexists('sxkj_offline_project')){
    $sql = "CREATE TABLE  ".tablename('sxkj_offline_project')." (
		   	`id` int(11) NOT NULL AUTO_INCREMENT,
		   	`taskid` int(11) DEFAULT '0',
  			`title` varchar(255) DEFAULT NULL,
  			`img` varchar(255) DEFAULT NULL,
  			`lists_img` text,
  			`commission` varchar(255) DEFAULT '',
  			`reprice` float(10,2) DEFAULT '0.00',
  			`package` text,
  			`content` text,
  			`path` varchar(255) DEFAULT NULL,
  			`cycle` int(11) DEFAULT '0',
  			`url` varchar(255) DEFAULT NULL,
  			`status` int(1) DEFAULT '0',
  			`add_time` int(11) DEFAULT '0',
  			`update_time` int(11) DEFAULT '0',
		  PRIMARY KEY (`id`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
	pdo_query($sql);
}

if(!pdo_tableexists('sxkj_offline_order')){
    $sql = "CREATE TABLE  ".tablename('sxkj_offline_order')." (
		   	`id` int(11) NOT NULL AUTO_INCREMENT,
  			`pid` int(11) DEFAULT '0',
  			`uid` int(11) DEFAULT '0',
  			`name` varchar(255) DEFAULT NULL,
  			`phone` varchar(255) DEFAULT NULL,
  			`order_num` varchar(255) DEFAULT NULL,
  			`out_trade_no` varchar(255) DEFAULT NULL,
  			`status` tinyint(1) DEFAULT '0',
  			`reprice` float(10,2) DEFAULT '0.00',
  			`commission` varchar(255) DEFAULT '',
  			`add_time` int(11) DEFAULT '0',
			`update_time` int(11) DEFAULT '0',
		  PRIMARY KEY (`id`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
	pdo_query($sql);
}

if(!pdo_tableexists('sxkj_offline_examine')){
    $sql = "CREATE TABLE  ".tablename('sxkj_offline_examine')." (
		   	`id` int(11) NOT NULL AUTO_INCREMENT,
		   	`type` int(1) DEFAULT '1' COMMENT '1-项目佣金2-分销佣金',
		   	`uid` int(11) DEFAULT '0' COMMENT '用户id',
		    `orderid` int(11) DEFAULT '0' COMMENT '订单id',
		    `phone` varchar(255) DEFAULT '' COMMENT '手机号',
		    `status` int(11) DEFAULT '0' COMMENT '1-通过2-拒绝',
		    `true_status` int(11) DEFAULT '0' COMMENT '-2已取消 -1审核失败 0-待审核 1-T0审核 2-T1审核 3-T2审核 4-T3审核 5-T4审核 6-T5审核',
		    `commission` float(10,2) DEFAULT '0.00' COMMENT '佣金',
		    `reason` varchar(255) DEFAULT '' COMMENT '审核原因',
		    `add_time` int(11) DEFAULT '0',
		  PRIMARY KEY (`id`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
	pdo_query($sql);
}

if(!pdo_tableexists('sxkj_offline_project_class')){
    $sql = "CREATE TABLE  ".tablename('sxkj_offline_project_class')." (
		   	`id` int(11) NOT NULL AUTO_INCREMENT,
  			`name` varchar(255) DEFAULT NULL,
  			`add_time` int(11) DEFAULT '0',
  			`update_time` int(11) DEFAULT '0',
		  PRIMARY KEY (`id`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
	pdo_query($sql);
}

if(!pdo_tableexists('sxkj_offline_data')){
    $sql = "CREATE TABLE  ".tablename('sxkj_offline_data')." (
		   	`id` int(11) NOT NULL AUTO_INCREMENT,
		   	`data` text,
  			`flag` varchar(255) DEFAULT NULL,
  			`add_time` int(11) DEFAULT '0',
  			`update_time` int(11) DEFAULT '0',
		  PRIMARY KEY (`id`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
	pdo_query($sql);
}

if(!pdo_tableexists('sxkj_offline_txrecord')){
    $sql = "CREATE TABLE  ".tablename('sxkj_offline_txrecord')." (
	    	`id` int(11) NOT NULL AUTO_INCREMENT,
		  	`uid` int(11) DEFAULT '0',
		  	`cash` varchar(20) DEFAULT '',
		  	`zfb` varchar(255) DEFAULT '',
		  	`true_name` varchar(255) DEFAULT '',
		  	`status` int(11) DEFAULT '0',
		  	`add_time` int(11) DEFAULT '0',
		  	`handle_time` int(11) DEFAULT '0',
		  PRIMARY KEY (`id`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
	pdo_query($sql);
}

if(!pdo_tableexists('sxkj_offline_user')){
    $sql = "CREATE TABLE  ".tablename('sxkj_offline_user')." (
		   	`id` int(11) NOT NULL AUTO_INCREMENT,
  			`openid` varchar(255) DEFAULT '',
  			`avatar` varchar(255) DEFAULT '',
  			`nickname` varchar(255) DEFAULT '',
  			`sex` int(1) DEFAULT '0',
  			`phone` varchar(50) DEFAULT '',
  			`cash` float(10,2) DEFAULT '0.00',
  			`is_distributor` tinyint(1) DEFAULT '0', 
  			`sup_promoterid` int(11) DEFAULT '0',
  			`jxscode` varchar(50) DEFAULT NULL,
  			`add_time` int(11) DEFAULT '0',
		  PRIMARY KEY (`id`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
	pdo_query($sql);
}

if(!pdo_tableexists('sxkj_offline_userteam')){
    $sql = "CREATE TABLE  ".tablename('sxkj_offline_userteam')." (
		   	`id` int(11) NOT NULL AUTO_INCREMENT,
  			`uid` int(11) DEFAULT '0',
  			`phone` varchar(20) DEFAULT NULL,
  			`true_name` varchar(255) DEFAULT NULL,
  			`remark` varchar(255) DEFAULT NULL,
  			`jxscode` varchar(255) DEFAULT NULL,
  			`status` tinyint(1) DEFAULT '0',
  			`add_time` int(11) DEFAULT '0',
		  PRIMARY KEY (`id`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
	pdo_query($sql);
}

if(!pdo_tableexists('sxkj_offline_banner')){
    $sql = "CREATE TABLE  ".tablename('sxkj_offline_banner')." (
		  `id` int(11) NOT NULL AUTO_INCREMENT,
		  `type` varchar(255) DEFAULT '',
		  `img` varchar(255) DEFAULT '',
		  `url` varchar(255) DEFAULT '',
		  `ewm` varchar(255) DEFAULT '',
		  `xh` int(11) DEFAULT 0,
		  `add_time` int(11) DEFAULT '0',
		  PRIMARY KEY (`id`)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
	pdo_query($sql);
}


if(!pdo_fieldexists('sxkj_offline_project', 'package')){
	$sql = "ALTER TABLE ".tablename('sxkj_offline_project')."  ADD `package` text";
	pdo_query($sql);
}

if(!pdo_tableexists('sxkj_offline_banner')){
    $sql = "CREATE TABLE  ".tablename('sxkj_offline_banner')." (
		  `id` int(11) NOT NULL AUTO_INCREMENT,
		  `type` varchar(255) DEFAULT '',
		  `img` varchar(255) DEFAULT '',
		  `url` varchar(255) DEFAULT '',
		  `ewm` varchar(255) DEFAULT '',
		  `xh` int(11) DEFAULT 0,
		  `add_time` int(11) DEFAULT '0',
		  PRIMARY KEY (`id`)
		) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
	pdo_query($sql);
}


/*12-16*/
if(!pdo_fieldexists('sxkj_offline_project', 'cid')){
	$sql = "ALTER TABLE ".tablename('sxkj_offline_project')."  ADD `cid` int(11) DEFAULT '0'";
	pdo_query($sql);
}

if(!pdo_fieldexists('sxkj_offline_project', 'start_time')){
	$sql = "ALTER TABLE ".tablename('sxkj_offline_project')."  ADD `start_time` int(11) DEFAULT '0'";
	pdo_query($sql);
}

if(!pdo_fieldexists('sxkj_offline_project', 'end_time')){
	$sql = "ALTER TABLE ".tablename('sxkj_offline_project')."  ADD `end_time` int(11) DEFAULT '0'";
	pdo_query($sql);
}

if(!pdo_fieldexists('sxkj_offline_project', 'note')){
	$sql = "ALTER TABLE ".tablename('sxkj_offline_project')."  ADD `note` text";
	pdo_query($sql);
}

if(!pdo_fieldexists('sxkj_offline_project', 'is_img')){
	$sql = "ALTER TABLE ".tablename('sxkj_offline_project')."  ADD `is_img` tinyint(1) DEFAULT 0";
	pdo_query($sql);
}

if(!pdo_fieldexists('sxkj_offline_project', 'is_txt')){
	$sql = "ALTER TABLE ".tablename('sxkj_offline_project')."  ADD `is_txt` tinyint(1) DEFAULT 0";
	pdo_query($sql);
}

if(!pdo_fieldexists('sxkj_offline_project', 'flag')){
	$sql = "ALTER TABLE ".tablename('sxkj_offline_project')."  ADD `flag` tinyint(1) DEFAULT 1";
	pdo_query($sql);
}

if(!pdo_fieldexists('sx_offline_order', 'text')){
	$sql = "ALTER TABLE ".tablename('sx_offline_order')."  ADD `text` text";
	pdo_query($sql);
}

if(!pdo_fieldexists('sx_offline_order', 'img')){
	$sql = "ALTER TABLE ".tablename('sx_offline_order')."  ADD `img` text";
	pdo_query($sql);
}
echo 'ok';exit;
?>